package com.qf.day08.bookstore.impl;

import com.qf.day08.bookstore.bookstore.BookStore;
import com.qf.day08.bookstore.service.BookStoreService;
import com.qf.day08.user.user.User;

import java.awt.print.Book;
import java.util.Arrays;

public class BookStoreImpl implements BookStoreService {
    private BookStore[] bookStore=new BookStore[1];
    private int count=0;

    public BookStoreImpl() {
    }

    public BookStoreImpl(BookStore[] bookStore, int count) {
        this.bookStore = bookStore;
        this.count = count;
    }

    public BookStore[] getBookStore() {
        return bookStore;
    }

    public void setBookStore(BookStore[] bookStore) {
        this.bookStore = bookStore;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    @Override

    public boolean addBook(BookStore book) {
        for (int i=0;i<count;i++) {
            if(book.getId().equals(bookStore[i].getId())){
                System.out.println("id重复,请重新添加");
                return false;
            }
        }
        if(count==bookStore.length){
            bookStore= Arrays.copyOf(bookStore,bookStore.length+5);
        }
            bookStore[count++]=book;

        for (int i=0;i<count;i++) {
            System.out.println("=====" + bookStore[i]);

        }
        for (int i=0;i<bookStore.length;i++) {
            System.out.println("=====" + bookStore[i]);

        }
        System.out.println(count);
        return true;
    }

    @Override
    public boolean updateBook(BookStore book) {
        return false;
    }

    @Override
    public boolean deleteBook(String bookId) {
        //定义删除id的索引
        int deleteIndex=0;
        //判断是否全部找完
        boolean flag=true;
        //新的数组
        BookStore[] newBookStore=new BookStore[count-1];
        //找到想删除id的索引
        for (int i=0;i<count;i++) {
            if(bookId.equals(bookStore[i].getId())){
                deleteIndex=i;
                System.out.println(deleteIndex);
                flag=false;
                break;
            }
        }
        //看看找完有没有此id的书
        if(flag){
            System.out.println("没有此id的书");
            return false;
        }
        count--;
        //给新数组赋值
        for (int i=0;i<count;i++) {
            if(i<deleteIndex){
                newBookStore[i]=bookStore[i];
            }else {
                //下一个为空,不让赋值
                newBookStore[i]=bookStore[i+1];
            }
        }

        bookStore=newBookStore;
        //测试
        for (int i=0;i<bookStore.length;i++) {
            System.out.println("=====" + bookStore[i]);
        }
        for (int i=0;i<bookStore.length;i++) {
            System.out.println("=====" + bookStore[i]);

        }
        System.out.println(count);
        return true;
    }

    @Override
    public Book selectOne(String bookId) {
        return null;
    }



}
